import {
  listReq
} from "@/api/authority_app/base";
import defaultSettings from "@/settings";
import {
  post
} from "@/utils/api.js";
// 改变弹窗表单 defaultAttr 结构中某个属性
export function changeAttrByPropGlobal(list, prop, nodeName, attriName, value) {
  let indexlist = list.map((item) => {
    return item.prop;
  });
  let index = indexlist.indexOf(prop);
  let item = list[index];
  this.$set(item[nodeName], attriName, value);
};
// 获取当前登录信息
export async function getUserInfoGlobal(queryUrl) {
  let mode = "result"
  let result = [];
  if (mode === "data" || !mode) {
    await listReq(queryUrl, null).then((res) => {
      result = res.result.data;
    });
  }
  if (mode === "result") {
    await listReq(queryUrl, null).then((res) => {
      result = Array(res.result);
    });
  }
  result = result[0];
  return result;
};
// 查询票据
export const getDraftById = data => post(
  `${defaultSettings.ctx.nbmsHtes}/s/dpcDraftInfo/getDraftInfoById?draftId=${data}`
);
// 打印 table
export function printTable(list, mode) {
  if (mode === "table" || !mode) {
    for (let i in list) {
      let t = list[i];
      let d = t.dictCode ? t.dictCode : "---";
      console.log(`|${t.prop}|${t.label}|${d}|`);
    }
  } else if (mode === "form") {
    for (let i in list) {
      let t = list[i];
      let d = t.dict ? t.dict : "---";
      let obj = t.type == "selectWithButton_Obj" ? t.prop : "---"
      let p = obj == "---" ? t.prop : "---";
      let url = "---"
      if (t.optionFormat) {
        url = t.optionFormat.queryUrl ? t.optionFormat.queryUrl : "---"
      }
      console.log(`|${p}|${obj}|${t.defaultAttr.label}|${d}|${url}|`);
      if (obj != "---") {
        for (let j in t.optionFormat.attriParams) {
          let ta = t.optionFormat.attriParams[j];
          console.log(`|${j}|${ta}|---|---|---|`);
        }
      }
    }
  }
};

export function createTable(list, mode) {
  if (mode == "table" || !mode) {
    list.forEach(i => {
      let t = i.data.attrs;
      let res = {};
      res["label"] = t["zh-name"];
      res["prop"] = t["eg-name"];
      res["width"] = "170";

      if (res["prop"] == "contractNo" ||
        res["prop"] == "draftNumber" ||
        res["prop"] == "cdRange") {
        res["width"] = "270";
      }

      if (t.hasOwnProperty("type")) {
        if (t.type == "currency") {
          res["type"] = "price";
        } else {
          res["type"] = t.type;
        }
        res["width"] = "120";
      }
      if (t.hasOwnProperty("dictionary")) {
        res["dictCode"] = t.dictionary;
      }
      console.log(res);
    });
  } else if (mode == "form") {
    list.forEach(i => {
      let t = i.data.attrs;
      let res = {
        defaultAttr: {}
      };
      res.defaultAttr["label"] = t["name"];
      res.defaultAttr["placeholder"] = `请输入${t["name"]}`;
      res.defaultAttr["size"] = "small";
      res["prop"] = t["ng-model"].split(".")[1];
      res["type"] = "input";
      if (t.hasOwnProperty("dictionary")) {
        res["dict"] = t.dictionary;
        res["type"] = "dict";
        res.defaultAttr["placeholder"] = `请选择${t["name"]}`
        res["selectOptionList"] = [];
      }
      if (t.hasOwnProperty("edit-type") && t["edit-type"] === 'currency') {
        res["type"] = "number";
        res.defaultAttr["isAmount"] = true;
      }
      if (t.hasOwnProperty("disable")) {
        res.defaultAttr["disabled"] = true;
      }
      if (t.hasOwnProperty("edit-type") && t["edit-type"] == "date") {
        res["type"] = "date";
        res.defaultAttr["valueFormat"] = "yyyyMMdd";
        res.defaultAttr["format"] = "yyyy-MM-dd";
      }
      // res.defaultAttr = JSON.stringify(res.defaultAttr)
      let s = {
        resStr: ""
      };
      transToStr(s, res)
      console.log(s.resStr);
    });
  }
}

function transToStr(s, Json) {
  s.resStr = s.resStr + "{";
  for (let p in Json) {
    if (typeof Json[p] == "object" && !Array.isArray(Json[p])) {
      s.resStr = s.resStr + "defaultAttr:";
      transToStr(s, Json[p]);
    } else if (p == "selectOptionList") {
      s.resStr = s.resStr + "selectOptionList:[],";
    } else if (typeof Json[p] == "boolean") {
      s.resStr = s.resStr + `${p}:true,`;
    } else {
      s.resStr = s.resStr + `${p}:"${Json[p]}",`;
    }
  }
  s.resStr = s.resStr + "},";
}


export function getDateStr(date) {
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let monthString = month < 10 ? `0${month}` : `${month}`;
  let dayString = day < 10 ? `0${day}` : `${day}`;
  let nowDate = `${date.getFullYear()}${monthString}${dayString}`;
  return nowDate;
}
